package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.WorkPostForm;

@Component
public class WorkPostFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return WorkPostForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		WorkPostForm form = (WorkPostForm) target;
		if (form.getTitle() == null || form.getTitle().isEmpty()) {
			errors.rejectValue("title", "empty");
		} else if (form.getTitle().length() > 100) {
			errors.rejectValue("tiltle", "tooLong");
		}
		if (form.getDescription() != null && form.getDescription().length() > 10000) {
			errors.rejectValue("description", "tooLong");
		}
		if ((form.getToUsers() == null || form.getToUsers().size() == 0) &&
				(form.getToProfiles() == null || form.getToProfiles().size() == 0)) {
			errors.rejectValue("toUsers", "empty");
		}
	}
}